<?php
class ctrl_price extends ctrl_base
{
	/**
	 * 产品分类
	 */
    public static function index() { 
    	$price_class_list = model_price::get_price_class_list();
    	self::smarty() -> assign('price_class_list',$price_class_list);
    	self::smarty() -> display('price_index.html');
    }
	/**
	 * 产品信息录入
	 */
    public static function query() { 
    	$id = isset($_GET['id'])? intval($_GET['id']):0;
    	if($id == 0) {
    		self::smarty()->display('error/wx_error.html');
    		exit;
    	}
    	$product_list = model_price::get_small_class_list($id);
    	//价格字段
    	$price_list = model_price::get_price_list();
    	$price_list_count = count($price_list);
    	$parent_price_list = array();
    	$children_price_list = array();
    	for($i=0;$i<$price_list_count;$i++) {
    		if($price_list[$i]['parent_id'] == 0) {
    			array_push($parent_price_list, $price_list[$i]);
    		} else {
    			array_push($children_price_list, $price_list[$i]);
    		}
    	}
    	    	
    	self::smarty() -> assign('product_list',$product_list);
    	self::smarty() -> assign('parent_price_list',$parent_price_list);
    	self::smarty() -> assign('children_price_list',$children_price_list);
    	self::smarty() -> display('price_query.html');
    }
    /**
     * 
     * 根据产品小类获取其他价格选项
     */
	public static function get_data() {
    	$product_id = isset($_GET['product_id'])? intval($_GET['product_id']):0;
    	if($product_id == 0) {
    		$result = array(
    			'errCode' => '01',
    			'errMsg' => '非法请求',
    			'items' => ''
    		);
    	} else {
    		$data['marerials'] = model_price::get_marerials($product_id);//产品材质
    		$area_price = model_price::get_area_price($product_id); //区域价格
    		$data['area_price'] = explode(",", $area_price);
    		$arr_transport = model_price::get_transport($product_id);
    		$data['transport'] = array();
    		$tranport_types = array(
    			'motor' => '汽运',
    			'shipping' => '船运',
    			'motor_shipping' => '汽船联运',
    			'train' => '火运'
    		);
    		foreach ($arr_transport as $key => $value) { //获取运输方式
    			if(!empty($value)) {
    				array_push($data['transport'], $key);
    			}
    		}
    		if(count($data['transport'])>0) {
	    		for($i=0;$i<count($data['transport']);$i++) {
	    			$transport_list['val'][$i] = $data['transport'][$i];
	    			$transport_list['name'][$i] = $tranport_types[$data['transport'][$i]];
	    		}
    		} else {
    			$transport_list = "";
    		}
    		$result = array(
    			'errCode' => '00',
    			'errMsg' => 'ok',
    			'marerials' => $data['marerials'], //基础材质
    			'area_price' => $data['area_price'], //区域定价
    			'transport' => $transport_list, //运输方式
    		);
    	}
    	framework_static_function::output_json($result,FALSE);
    }
    /**
     * 获取运输区域
     */
    public static function get_transport_area() {
    	$product_id = isset($_GET['product_id'])? intval($_GET['product_id']):0;
    	$filed = isset($_GET['filed'])? trim($_GET['filed']):"";
    	if($product_id == 0 || empty($filed)) {
    		$result = array(
    			'errCode' => '01',
    			'errMsg' => '非法请求',
    			'items' => ''
    		);
    	} else {
    		$rs = model_price::get_filed($product_id, $filed);
    		if(count($rs)>0) {
    			$data = explode(",", $rs);
    			$result = array(
	    			'errCode' => '00',
	    			'errMsg' => 'ok',
	    			'items' => $data
	    		);
    		} else {
    			$result = array(
	    			'errCode' => '02',
	    			'errMsg' => '不存在该字段',
	    			'items' => ''
	    		);
    		}
    	}
    	framework_static_function::output_json($result,false);
    }
	/**
	 * 价格查询结果展示
	 */
    public static function query_result() {
    	$pageArray['product_id'] = isset($_POST['product_id'])? intval($_POST['product_id']):"";
    	$pageArray['product_name'] = isset($_POST['product_name'])? trim($_POST['product_name']):"";
    	$pageArray['marerials'] = isset($_POST['marerials'])? trim($_POST['marerials']):"";
    	$pageArray['format'] = isset($_POST['format'])? trim($_POST['format']):"";
    	$pageArray['area_price'] = isset($_POST['area_price'])? trim($_POST['area_price']):"";
    	$pageArray['customer_type'] = isset($_POST['customer_type'])? trim($_POST['customer_type']):"";
    //	$pageArray['price_list'] = isset($_POST['price_list'])? $_POST['price_list']:"";
    	$pageArray['transport'] = isset($_POST['transport'])? trim($_POST['transport']):"";
    	$pageArray['transport_type'] = isset($_POST[$pageArray['transport']])? trim($_POST[$pageArray['transport']]):"";
    	$pageArray['number'] = isset($_POST['number'])? floatval(($_POST['number'])):0.0;
    	
    	$pageArray['total_price'] = model_price::calc_total_price($pageArray);
    	
    	$cell_price = 3000;
    	$pageArray['total_price'] = $cell_price * $pageArray['number'];
    	
    	self::smarty() -> assign("pageArray",$pageArray);
    	self::smarty() -> display('price_query_result.html');
    }
       
}
